package service;

import dao.UserDao;
import domain.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserService implements UserDao {
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> getAllUser() {
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user=new User();
        user.setAccount("2016014308");
        user.setPassword("1234");
        user.setSex("女");
        user.setName("测试");
        mapper.add(user); // 这里可以执行成功
        user.setAccount("2016014307");
        mapper.add(user); // 这里不会执行成功，因而导致前面进行事务回滚
        return mapper.getAllUser();
    }

    @Override
    public User query(String account) {
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        return mapper.query(account);
    }


    @Override
    public int add(User user) {
        UserDao mapper=sqlSession.getMapper(UserDao.class);
        return mapper.add(user);
    }

    @Override
    public int update(User user) {
        UserDao mapper=sqlSession.getMapper(UserDao.class);
        return mapper.update(user);
    }

    @Override
    public int delete(String account) {
        UserDao mapper=sqlSession.getMapper(UserDao.class);
        return mapper.delete(account);
    }
}
